package com.artisan.spider;

import com.artisan.spider.consts.Consts;
import com.artisan.spider.domain.SpDictionary;
import com.artisan.spider.util.UuidUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.microsoft.playwright.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Zhuaqu {


   public static void main(String[] args) {

        try (Playwright playwright = Playwright.create()) {
            String pageUrl = Consts.zysearch;
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate(pageUrl);


            List<SpDictionary> allList = new ArrayList();
            List<SpDictionary> firstList = new ArrayList();
            List<SpDictionary> sendtList = new ArrayList();


            //效力级别
            Locator xljb = page.locator("#WebUCSearchFrom1_divXiaoLi > select > option");
            if (xljb.count() > 0) {
                for (int i = 0; i < xljb.count(); i++) {
                    String text = xljb.nth(i).innerText();
                    String value = xljb.nth(i).getAttribute("value");
                    log.info("效力级别*****{}---》{}", text, value);

                    SpDictionary spDictionary = new SpDictionary();
                    spDictionary.setDictName(text);
                    spDictionary.setNameCode(value);
                    spDictionary.setDictMark("zy_xljb");
                    spDictionary.setId(UuidUtils.getUUid());
                    if (value.length() == 4 || StringUtils.isBlank(value)) {
                        spDictionary.setParentId("0");
                        firstList.add(spDictionary);
                    } else {
                        sendtList.add(spDictionary);
                    }
                    allList.add(spDictionary);
                }
                for (SpDictionary first : firstList) {
                    for (SpDictionary second : sendtList) {
                        if (second.getNameCode().substring(0, 4).equals(first.getNameCode())) {
                            second.setParentId(first.getParentId());
                        }
                    }
                }
                System.out.println("sadas");
            }
        }
    }
}
